package com.sidianzhong.sdz.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sidianzhong.sdz.model.WebInspectiondatarecord;
import com.sidianzhong.sdz.model.WebInspectionrecord;
import com.sidianzhong.sdz.model.WebInspectionstaffrecord;
import com.sidianzhong.sdz.service.WebInspectiondatarecordService;
import com.sidianzhong.sdz.service.WebInspectionrecordService;
import com.sidianzhong.sdz.service.WebInspectionstaffrecordService;
import com.sidianzhong.sdz.utils.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Api(description = "提交记录")
@RestController
public class SaveController {

    @Autowired
    WebInspectionrecordService webInspectionrecordService;
    @Autowired
    WebInspectionstaffrecordService webInspectionstaffrecordService;
    @Autowired
    WebInspectiondatarecordService webInspectiondatarecordService;

    @ApiOperation(value = "提交巡查记录")
    @RequestMapping(value = "/saveAll",
            method = RequestMethod.POST,
            produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object saveAll(
            HttpServletRequest request,
            @RequestParam(value = "json", required = false) String json

    ) {
//      json = "{\"data\":[{\"equipmentkey\":\"1\",\"equipmenttypekey\":\"8\",\"itemvalue\":1.0},{\"equipmentkey\":\"1\",\"equipmenttypekey\":\"8\",\"itemvalue\":0.0},{\"equipmentkey\":\"1\",\"equipmenttypekey\":\"8\",\"itemvalue\":0.0},{\"equipmentkey\":\"1\",\"equipmenttypekey\":\"8\",\"itemvalue\":0.0},{\"equipmentkey\":\"1\",\"equipmenttypekey\":\"8\",\"itemvalue\":0.0},{\"equipmentkey\":\"1\",\"equipmenttypekey\":\"8\",\"itemvalue\":0.0},{\"equipmentkey\":\"1\",\"equipmenttypekey\":\"8\",\"itemvalue\":0.0},{\"equipmentkey\":\"1\",\"equipmenttypekey\":\"8\",\"itemvalue\":0.0},{\"equipmentkey\":\"1\",\"equipmenttypekey\":\"8\",\"itemvalue\":0.0},{\"equipmentkey\":\"1\",\"equipmenttypekey\":\"8\",\"itemvalue\":0.0}],\"submitBack\":\"嗯\",\"userName\":\"王森\"}";

        JSONObject jsonObject = JSONObject.parseObject(json);
        JSONArray userName = jsonObject.getJSONArray("userName");
        String submitBack = jsonObject.getString("submitBack");
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        JSONArray data = jsonObject.getJSONArray("data");

        //提交巡机结果
        WebInspectionrecord item = new WebInspectionrecord();
        Date date = new Date();
        if( time != null ){
            item.setRecordtime(time);
        }
        if( submitBack != null ){
            item.setRecordnote(submitBack);
        }
        WebInspectionrecord record = webInspectionrecordService.create(item);
        //创建巡机人员

        List<WebInspectionstaffrecord> collect = userName.stream().map(s -> {
            WebInspectionstaffrecord webInspectionstaffrecord = new WebInspectionstaffrecord();
            if (record.getId() != null) {
                webInspectionstaffrecord.setRecordid(record.getId());
            }
            if (s.toString() != null) {
                webInspectionstaffrecord.setStaffname(s.toString());
            }
            return webInspectionstaffrecord;
        }).collect(Collectors.toList());

        int i = webInspectionstaffrecordService.batchInsert(collect);

        //提交巡机记录数据
        List<WebInspectiondatarecord> collect1 = data.stream().map(ss -> {
            String s1 = JSONObject.toJSONString(ss);
            JSONObject jsonObject1 = JSONObject.parseObject(s1);
            WebInspectiondatarecord webInspectiondatarecord = new WebInspectiondatarecord();
            webInspectiondatarecord.setRecordid(item.getId());
            webInspectiondatarecord.setEquipmentkey(jsonObject1.getString("equipmentkey"));
            webInspectiondatarecord.setEquipmenttypekey(jsonObject1.getString("equipmenttypekey"));
            webInspectiondatarecord.setItemid(jsonObject1.getInteger("id"));
            webInspectiondatarecord.setItemvalue(jsonObject1.getString("itemvalue"));
            return webInspectiondatarecord;
        }).collect(Collectors.toList());

        int i1 = webInspectiondatarecordService.batchInsert(collect1);
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("result",true);
        return new ResponseEntity<>(ResultModel.ok(jsonObject1), HttpStatus.OK);
    }
}
